from .golrity import API
from . import odoo_fields as fields


import logging

_logger = logging.getLogger(__name__)


class BaseModel(object):
    @classmethod
    def value_get(cls, record):
        one = {}
        for fld, val in record.items():
            if hasattr(cls, fld):
                meta = getattr(cls, fld)
                one[meta.field_name] = meta.value_get(val)
        return one


class ApiFapiao(BaseModel):
    # OCR 数据, 写入 odoo 前. 转换为 odoo 需要的格式
    # 对应 fp.fapiao

    invoice_type = fields.Selection(
        selection=[
            ('10100', 'vat_special'),  # '增值税专用发票',
            ('10101', 'vat_general'),  # '增值税普通发票',
            ('10102', 'vat_general_e'),  # '增值税电子普通发票',

            # ('10103', 'vat_general_roll'),  # '增值税普通发票(卷票)',
            # ('10104', 'vat_vehicle'),  # '机动车销售统一发票',
            # ('10105', 'vat_vehicle_2'),  # '二手车销售统一发票',

            # ('10200', 'asdad'),  # '定额发票',
            # ('10400', 'asdad'),  # '机打发票',
            # ('10500', 'asdad'),  # '出租车发票',
            # ('10503', 'asdad'),  # '火车票',
            # ('10505', 'asdad'),  # '客运汽车',
            # ('10505a', 'asdad'),  # '船票'
            # ('10506', 'asdad'),  # '航空运输电子客票行程单',
            # ('10507', 'vat_toll'),  # '过路费发票',
            # ('10900', 'asdad'),  # '可报销其他发票',
            # ('10902', 'asdad'),  # '完税证明',
            # ('20100', 'asdad'),  # '国际小票',
            # ('20105', 'asdad'),  # '滴滴出行行程单',


            # ('01', 'vat_special'),  # 增值税专用发票
            # ('02', 'vat_traffic'),  # 货物运输业增值税专用发票
            # ('03', 'vat_vehicle'),  # 机动车销售统一发票
            # ('04', 'vat_general'),  # 增值税普通发票
            # ('10', 'vat_general_e'),  # 增值税普通发票(电子)
            # ('11', 'vat_general_roll'),  # 增值税普通发票(卷式)
            # ('14', 'vat_toll'),  # 通行费发票
            # ('15', 'vat_vehicle_2'),  # 二手车发票
            # ('08', 'vat_special_e'),  # 增值税电子专用发票
            # ('16', 'vat_block_chain'),  # 区块链电子发票 只支持深圳地区
            # ('21', 'vat_special_ex'),  # 全电专票
            # ('22', 'vat_general_ex'),  # 全电普票
        ],
        string="发票种类", field_name='invoice_type')

    machine_code = fields.Char('机器编号', 'taxmachine_code')
    code = fields.Char('发票代码', 'invoice_code')
    number = fields.Char('发票号码', 'invoice_number')
    check_code = fields.Char('校验码', 'check_code')

    seller = fields.Char('销售方名称', 'saler_name')
    seller_tax_id = fields.Char('销售方识别号', 'saler_tin')

    buyer = fields.Char('购买方名称', 'buyer_name')
    buyer_tax_id = fields.Char('购买方识别号', 'buyer_tin')

    remark = fields.Char('备注', 'note')

    issuer = fields.Char('开票人', 'drawer_name')
    receiptor = fields.Char('收款人', 'payee_name')
    reviewer = fields.Char('复核', 'checker_name')

    date = fields.DateYYYYCnMMCnDDCn('开票时间', 'date_fapiao')

    tax = fields.Float('税额', 'amount_tax')
    total = fields.Float('含税金额', 'amount_total')
    pretax_amount = fields.Float('不含税金额', 'amount_untaxed')
    total_cn = fields.Char('含税金额大写', 'cn_amount_total')

    # 以上是 票面信息

    company_seal = fields.BooleanChar01(
        '是否有公司印章', 'company_seal', help="0: 没有; 1: 有")

    kind = fields.Char('发票消费类型', 'kind')
    service_name = fields.Char('服务类型', 'service_name')
    province = fields.Char('省', 'province')
    city = fields.Char('市', 'city')
    qrcode = fields.CharList('二维码', 'qrcode')

    @classmethod
    def value_get(cls, record):
        one = super().value_get(record)
        return one

    @classmethod
    def after_read(cls, records):
        return [cls.value_get(record) for record in records]


class OdooApi(API):
    def ocr_one(self, record):
        content = record
        res = self.get_item_info(content)
        # print('ocr one 2,', res)
        _logger.info('ocr fapiao ok, %s',  res)

        records2 = ApiFapiao.after_read([res])
        # print('ocr one 3ok,', records2[0])

        return {'result': records2[0]}
